import Vue from 'vue';
import axios from 'axios';
import router from '../router';
Vue.use(router);
Vue.prototype.$axios = axios;

const myconfig = {
  type: "Bearer",
  header: {
    token: sessionStorage.getItem("token")
 }
};
//请求前拦截加入token
axios.interceptors.request.use(function (config) {
  if(!sessionStorage.getItem('token')){
    console.log('拦截器没有token')
   }else{
    config.headers.Authorization = myconfig.type + ' ' + sessionStorage.getItem('token');
   }
   return config;
}, function (err) {
  return Promise.reject(err);
});
//请求后拦截处理数据
axios.interceptors.request.use(function (result){
     if(result){

     }
     return result;
  },function (err) {
   return Promise.reject(err);
});


//发送请求
export function post (url, params) {
  return new Promise((resolve, reject) => {
   axios
    .post(url, params)
    .then(
     res => {
      resolve(res.data)
     },
     err => {
      reject(err.data)
     }
    )
    .catch(err => {
     reject(err.data)
    })
  })
 }
 export function get (url, params) {
  return new Promise((resolve, reject) => {
   axios
    .get(url, {
     params: params
    })
    .then(res => {
     resolve(res.data)
    })
    .catch(err => {
     reject(err.data)
    })
  })
 }
 export function put (url, params) {
  return new Promise((resolve, reject) => {
   axios
    .put(url, {
     params: params
    })
    .then(res => {
     resolve(res.data)
    })
    .catch(err => {
     reject(err.data)
    })
  })
 }
